import 'package:flutter/material.dart';

/// 版本信息模型 - Version Information Model
/// 表示单个版本的详细信息
class VersionInfo {
  final String id;
  final String version;
  final DateTime createdAt;
  final int contentLength;
  final String notes;
  final String contentPreview;
  final bool isAutoSave;

  VersionInfo({
    required this.id,
    required this.version,
    required this.createdAt,
    required this.contentLength,
    required this.notes,
    required this.contentPreview,
    required this.isAutoSave,
  });

  /// 获取内容预览 - Get Content Preview
  String getContentPreview() => contentPreview;

  /// 获取内容长度 - Get Content Length
  int getContentLength() => contentLength;
}

/// 版本对比结果模型 - Version Comparison Result Model
/// 表示两个版本之间的差异对比结果
class VersionComparisonResult {
  final VersionInfo oldVersion;
  final VersionInfo newVersion;
  final List<DiffLine> differences;
  final int addedLines;
  final int removedLines;
  final int modifiedLines;

  VersionComparisonResult({
    required this.oldVersion,
    required this.newVersion,
    required this.differences,
    required this.addedLines,
    required this.removedLines,
    required this.modifiedLines,
  });

  /// 获取总变更行数 - Get Total Changed Lines
  int getTotalChanges() => addedLines + removedLines + modifiedLines;

  /// 获取变更摘要 - Get Change Summary
  String getChangeSummary() {
    final changes = <String>[];
    if (addedLines > 0) changes.add('+$addedLines');
    if (removedLines > 0) changes.add('-$removedLines');
    if (modifiedLines > 0) changes.add('~$modifiedLines');
    return changes.join(' ');
  }
}

/// 差异行类型枚举 - Diff Line Type Enum
enum DiffType {
  added,    // 新增行 - Added line
  removed,  // 删除行 - Removed line
  modified, // 修改行 - Modified line
  unchanged // 未变更行 - Unchanged line
}

/// 差异行模型 - Diff Line Model
/// 表示版本对比中的单行差异
class DiffLine {
  final int lineNumber;
  final String content;
  final DiffType type;
  final String? oldContent; // 用于修改行的原始内容 - Original content for modified lines

  DiffLine({
    required this.lineNumber,
    required this.content,
    required this.type,
    this.oldContent,
  });

  /// 获取行类型颜色 - Get Line Type Color
  Color getTypeColor() {
    switch (type) {
      case DiffType.added:
        return Colors.green;
      case DiffType.removed:
        return Colors.red;
      case DiffType.modified:
        return Colors.orange;
      case DiffType.unchanged:
        return Colors.grey;
    }
  }

  /// 获取行类型图标 - Get Line Type Icon
  IconData getTypeIcon() {
    switch (type) {
      case DiffType.added:
        return Icons.add;
      case DiffType.removed:
        return Icons.remove;
      case DiffType.modified:
        return Icons.edit;
      case DiffType.unchanged:
        return Icons.horizontal_rule;
    }
  }
}

/// 版本管理状态类 - Version Management State Class
/// 管理版本页面的所有状态数据
class VersionState {
  // 控制器 - Controllers
  final TextEditingController searchController = TextEditingController();

  // 版本数据 - Version Data
  List<VersionInfo> versions = [];
  List<VersionInfo> filteredVersions = [];
  List<String> selectedVersions = [];

  // UI 状态 - UI State
  bool isLoading = false;
  bool isSearchVisible = false;
  String searchQuery = '';
  String sortOrder = 'time_desc'; // time_desc, time_asc, version_desc, version_asc
  int todayVersionCount = 0;

  // 版本对比状态 - Version Comparison State
  VersionComparisonResult? comparisonResult;
}